<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input v-model="queryParam.name" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item allowClear label="商圈" >
              <a-select v-model="queryParam.companyId" v-decorator="[ 'companyId']">
                <a-select-option v-for="item in companys" :key="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!--<a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select placeholder="请选择" default-value="0">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">关闭</a-select-option>
                <a-select-option value="2">运行中</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>-->
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true, clear: true }"
      :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
    >
      <span slot="statusType" slot-scope="text">
        {{ text | statusFilter }}
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a v-action:update @click="updateStatus(record)"> {{record.status ==0 ? '上架':'下架'}}</a>
          <a-divider type="vertical"/>
          <a v-action:delete @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>

  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import Edit from './Edit'
  import {selectPage, del,updateStatus } from '@/api/market/bigwheel'
  import { selectAll } from '@/api/companyService/company'

  export default {
    components: {
      STable,
      Edit
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        companys:[],
        // 表头
        columns: [
          {
            title: '商圈名称',
            dataIndex: 'companyName'
          },
          {
            title: '名称',
            dataIndex: 'name'
          },
          {
            title: '开始时间',
            dataIndex: 'beginTime'
          },
          {
            title: '截至时间',
            dataIndex: 'endTime'
          },
          {
            title: '是否有效',
            dataIndex: 'status',
            scopedSlots: { customRender: 'statusType' }
          },
          {
            title: '操作',
            width: '180px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: []
      }
    },
    //--############  data() ###############-->
    created() {
      // this.getAllMerchant()
      selectAll().then(res => {
        this.companys = res.result
      })
    },

    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      updateStatus(record){
        // 0未上架，1上架
        let status = 1
        if (record.status == 1) {
          status = 0
        } else {
          status = 1
        }
        const body = {
          id: record.id,
          status: status
        }

        updateStatus(body).then(res => {
          if (res.code === 200) {
            this.$message.success('操作成功！')
            //刷新页面
            this.$refs.table.refresh()
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      }
    },

    filters: {
      statusFilter(state) {
        const statusMap = {
          0: '已下架',
          1: '已上架'
        }
        return statusMap[state]
      }
    }
    //--############  filters() ###############-->
  }
</script>
